package descentFly.app.managePanel.chat
{
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.base.uif_source;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_input;
	import UIFree.controls.uif_scrollArea;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import descentFly.app.df_block;
	import descentFly.app.df_component;
	import descentFly.app.df_events;
	import descentFly.app.df_label;
	import descentFly.app.df_list;
	import descentFly.app.df_panel;
	import descentFly.app.managePanel.chat.df_userList;
	import descentFly.away3D4.net.df_netSupport;
	import descentFly.controller.df_userController;
	import descentFly.controller.myFlyController;
	import descentFly.df_utils;
	import descentFly.server.df_servers;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class df_chat extends df_panel {
		public static var DEFAULT_LOGO		: String = df_servers.DATA_URL + 'images/nophoto.jpg';
		
		private var _userList	: df_userList;
		private var _chatList	: df_turnList;
		private var _input		: uif_input;
		private var _autor		: Object;
		
		public function df_chat(parent:uif_component, a_autor: Object, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='chat') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			social.getUserFromObj(a_autor, function(user: Object): void {
				_autor = user;
			});
		}
		
		override protected function listenEvents():void {
			addEvent(df_events.RECEIVECHATMESSAGE, doReceive, app.stage);
			addEvent(df_events.ENTERUSER, doEnterUser, app.stage);
			addEvent(df_events.EXITUSER, doExitUser, app.stage);
			addEvent(df_events.KILLER, doKiller, app.stage);
			addEvent(df_netSupport.ENTER_SESSION, doEnterSession, app.stage);
			addEvent(Event.CLEAR, doSpaceClear, Space);
		}
		
		private function doSpaceClear(e: Event): void {
			_userList.scrollArea.disposeChilds();
		}
		
		override protected function createComponents(layer:df_component, inner:Rectangle):void {
			var space: int =  getSpace(true) / 2;
			var r1: Array = df_utils.rectDivY(inner, 0.3, space);
			var rb: Array = df_utils.rectDivX(r1[0], 0.8, space);
//			var rbl: Array = df_utils.rectDivY(rb[1], 0.4, space);
			
			var tblock: df_block = new df_block(layer, r1[1].x, r1[1].y, r1[1].width, r1[1].height, '');
			var rt: Array = df_utils.rectDivX(tblock.getInner(), 0.3, space);
			
			_userList = new df_userList(tblock, rt[0].x, rt[0].y, rt[0].width, rt[0].height, 'chatList');
			
			_chatList = new df_turnList(tblock, rt[1].x, rt[1].y, rt[1].width, rt[1].height, 'chatList');
			_chatList.scrollArea.maxCount = 20;
			
//			_image = new uif_source(layer, rb[0].x, rb[0].y, rb[0].height, rb[0].height, style.image);
			
			
			var button: uif_gbutton = new uif_gbutton(layer, rb[1].x + (rb[1].width - se_util.DEFWIDTHBUTTON)/2, 
															rb[1].y + (rb[1].height - se_util.DEFHEIGHTBUTTON)/2, 
				se_util.DEFWIDTHBUTTON, se_util.DEFHEIGHTBUTTON, 'button');
			button.text = locale.SEND;
			button.addEvent(MouseEvent.CLICK, doSendMessage);
			
//			(new df_label(layer, rbl[1].x, rbl[1].y, rbl[1].width, rbl[1].height, 'description')).text = locale.CHATDESCR;
			
			_input = new uif_input(layer, rb[0].x, rb[0].y, rb[0].width, rb[0].height, 'input');
			_input.addEvent(KeyboardEvent.KEY_DOWN, doKey);
			_input.addEvent(FocusEvent.FOCUS_IN, doFocusIn);
			_input.addEvent(FocusEvent.FOCUS_OUT, doFocusOut);
		}
		
		protected function doFocusIn(e: Event): void {
			stage.dispatchEvent(new ObjectEvent(myFlyController.FREEZEMYCONTROLLER, true));
			Space.myChip.dispatchEvent(new ObjectEvent(df_userController.ACTION_REQUIRE, {
				action	: df_userController.ACTION_NOACTIVE,
				state	: true
			}));
		}
		
		protected function doFocusOut(e: Event): void {
			stage.dispatchEvent(new ObjectEvent(myFlyController.FREEZEMYCONTROLLER, false));
			Space.myChip.dispatchEvent(new ObjectEvent(df_userController.ACTION_REQUIRE, {
				action	: df_userController.ACTION_NOACTIVE,
				state	: false
			}));
		}
		
		protected function doKey(e: KeyboardEvent): void {
			if ((e.keyCode == 13) && (e.ctrlKey)) doSendMessage();
		}
		
		protected function doSendMessage(e: Event=null): void {
			stage.dispatchEvent(new ObjectEvent(df_events.SENDTOCHAT, {
				uid		: _autor.uid,
				text	: _input.text
			}));
			_input.text = '';
		}
		
		public function addMessage(user: Object, text: String): void {
			social.getUserFromObj(user, function(a_user: Object): void {
				new df_chatItem(_chatList.scrollArea, a_user, text, 'chatItem');
			});
		}
		
		protected function doEnterUser(e: ObjectEvent): void {
			social.getUserFromObj(e.object.uid, function(user: Object): void {
				social.getUserTitle(user, function(title: String): void {
					addSystemMessage(Utils.sprintf(locale.ENTER_USER, title));
				});
			});			
			_userList.addUser(e.object.uid);   
		}
		
		protected function doExitUser(e: ObjectEvent): void {
			social.getUserFromObj(e.object.uid, function(user: Object): void {
				social.getUserTitle(user, function(title: String): void {
					addSystemMessage(Utils.sprintf(locale.EXIT_USER, title));
				});
			});
			
			_userList.removeUser(e.object.uid);
		}
		
		protected function doKiller(e: ObjectEvent): void {
			social.getUserFromObj(e.object.killer, function(user1: Object): void {
				social.getUserTitle(user1, function(title1: String): void {
					
					social.getUserFromObj(e.object.uid, function(user2: Object): void {
						social.getUserTitle(user2, function(title2: String): void {
							addSystemMessage(Utils.sprintf(locale.KILLEACT, [title1, title2]));
						});
					});
					
				});
			});			
		}
		
		protected function addSystemMessage(text: String): void {
			new df_chatSystem(_chatList.scrollArea, text, 'chatItem');
		}
		
		protected function doReceive(e: ObjectEvent): void {
			addMessage(e.object.uid, e.object.text);
		}
		
		protected function doEnterSession(e: ObjectEvent): void {
			addSystemMessage(locale.ENTER_SESSION);
			for (var uid: String in e.object.users) {
				_userList.addUser(uid);
			}
			_userList.addUser(_autor.uid);
		}
	}
}